<?php
session_start();
if(isset($_SESSION['admin_id'])) {
    $mes="";
    if (isset($_SESSION['mes'])) {
        $mes = $_SESSION['mes'];
        $_SESSION['mes']="";
    }
    include ("../include/menu.php");
    include ("../include/dbFunctions.php");
    include ("../include/formFunctions.php");

    $query = "SELECT DISTINCT(e.event_name), e.event_status, e.event_id FROM event e, session s
        WHERE event_status = 'confirmed'
        ORDER BY event_status DESC";
    $event_array = executeSelectQuery($query);
    ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>JA Scheduling</title>

        <script type="text/javascript" language="javascript" src="../media/js/jquery/jquery-1.6.1.min.js"></script>
        <script type="text/javascript" language="javascript" src="../media/js/jquery/dataTable.js"></script>
        <script type="text/javascript" src="../media/js/jquery/addInPlace.js"> </script>

        <!--  checkbox styling script -->
        <script src="../media/js/jquery/ui.core.js" type="text/javascript"></script>
        <script src="../media/js/jquery/ui.checkbox.js" type="text/javascript"></script>
        <script src="../media/js/jquery/jquery.bind.js" type="text/javascript"></script>


        <!--  styled select box script version 2 -->
        <script src="../media/js/jquery/jquery.selectbox-0.5_style_2.js" type="text/javascript"></script>


        <!--  CSS script -->
        <link rel="stylesheet" href="../media/css/screen.css" type="text/css" media="screen" title="default" />
        <link type="text/css" href="../media/css/dataTable.css" rel="stylesheet" />

        <!-- Manage table script -->
        <script type="text/javascript" charset="utf-8">
            $(document).ready(function() {
                $('#admintable').dataTable( {
                    "sPaginationType": "full_numbers"
                } );
            } );
        </script>

    </head>
    <body>
        <!-- DON'T TOUCH! Start: page-top-outer -->
            <?php
            top_outer();
            ?>
        <!-- End: page-top-outer -->

        <div class="clear">&nbsp;</div>

        <!--  start nav-outer-repeat................................................................................................. START -->
        <div class="nav-outer-repeat">
            <!--  start nav-outer -->
            <div class="nav-outer">

                <!-- start nav-right -->
                    <?php
                    nav_right();
                    ?>
                <!-- end nav-right -->


                <!--  start nav -->
                <div class="nav">
                    <div class="table">

                            <?php
                            navigation_event();
                            ?>

                        <div class="clear"></div>
                    </div>
                    <div class="clear"></div>
                </div>
                <!--  start nav -->

            </div>
            <div class="clear"></div>
            <!--  start nav-outer -->
        </div>
        <!--  start nav-outer-repeat................................................... END -->

        <div class="clear"></div>

        <!-- start content-outer ........................................................................................................................START -->
        <div id="content-outer">
            <!-- start content -->
            <div id="content">

                <!--  start page-heading --> <!--lyedit-->
                <div id="page-heading">
                    <h1>Manage Upcoming & Ongoing Courses</h1>
                </div>
                <!-- end page-heading -->

                <table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
                    <tr>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
                        <th class="topleft"></th>
                        <td id="tbl-border-top">&nbsp;</td>
                        <th class="topright"></th>
                        <th rowspan="3" class="sized"><img src="../media/images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
                    </tr>
                    <tr>
                        <td id="tbl-border-left"></td>
                        <td>
                            <!--  start content-table-inner ...................................................................... START -->
                            <div id="content-table-inner">
                                <h2></h2>
                                <!--  start table-content  -->

                                <div id="table-content">
                                    <!--  Can put table start from here within this div--> <!--lyedit-->

                                        <?php echo "<b><font color = red>$mes</font></b>"?>
                                    <table cellpadding="0" cellspacing="0" border="0" class="display" id="admintable">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Course</th>
                                                <th>No. Class</th>
                                                <th>No. Students</th>
                                                <th>Tasks After Course</th>
                                                <th>Start Date</th>
                                                <th>End Date</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                                <?php
                                                for ($i=0;$i<count($event_array);$i++) {
                                                        $id = $event_array[$i]['event_id'];

                                                        //no student & no class
                                                        $query3= "SELECT class_no_student FROM class WHERE event_id = $id";
                                                        $student_array = executeSelectQuery($query3);
                                                        $no_class = count($student_array);
                                                        $no_student = 0;
                                                        for ($k=0;$k<$no_class;$k++) {
                                                            $no_student +=$student_array[$k]['class_no_student'];
                                                        }
                                                        //task
                                                        $query= "SELECT *  FROM track_detail d,track_criteria t
                                                                WHERE d.track_id=t.track_id
                                                                AND d.event_id = $id
                                                                AND t.track_description ='after'";
                                                        $task_array = executeSelectQuery($query);
                                                        $taskall = count($task_array);
                                                        $done = 0;
                                                        for ($l=0;$l<$taskall;$l++) {
                                                            if ($task_array[0]['track_detail']==1) {
                                                                $done+=1;
                                                            }
                                                        }
                                                        if ($taskall==$done) {
                                                            $track_status  = "<img src='../media/images/forms/check_icon.png' height ='20'>";
                                                        } else {
                                                            $track_status="<img src='../media/images/forms/cross_icon.png' height ='20'>";
                                                        }
                                                        //first last
                                                        $query51 = "SELECT * FROM session WHERE event_id = ".$id;
                                                        $session_array = executeSelectQuery($query51);
                                                        $first = $last = $session_array[0]['session_date'];
                                                        for ($m=0;$m<count($session_array);$m++) {
                                                            if ($session_array[$m]['session_date']<$first) {
                                                                $first = $session_array[$m]['session_date'];
                                                            }
                                                            if ($session_array[$m]['session_date']>$last) {
                                                                $last = $session_array[$m]['session_date'];
                                                            }
                                                        }
                                                         $today = date("Y-m-d");
                                                        if ($first>$today) {
                                                            $status="Upcoming";
                                                        } elseif ($last>$today) {
                                                            $status ="Ongoing";
                                                        } else {
                                                            $status="Past";
                                                        }
                                                    if ($status!='Past') {
                                                        
                                                        $no = $i+1;
                                                        $event = $event_array[$i]['event_name'];
                                                        //
                                                        
                                                        echo "<tr>";
                                                        echo "<td>$no</td>";
                                                        echo "<td><a href='eventView.php?id=".$id."'>$event</a></td>";
                                                        echo "<td>$no_class</td>";
                                                        echo "<td>$no_student</td>";
                                                        echo "<td>$track_status</td>";
                                                        echo "<td>$first</td>";
                                                        echo "<td>$last</td>";
                                                        echo "<td>$status</td>";
                                                        ?>

                                            <td><a href='action/eventCancelEvent.php?id= <?php echo $id?>' onclick="return  confirm('This event status will be changed to Canceled. Do you want to continue?')">Cancel</a></td>
                                                        <?php
                                                    }
                                                }
    
    ?>
                                        </tbody>
                                    </table>

                                    <!--  end table-content here -->
                                </div>
                                <div class="clear"></div>
                            </div>
                            <!--  end content-table-inner ............................................END  -->
                        </td>
                        <td id="tbl-border-right"></td>
                    </tr>
                    <tr>
                        <th class="sized bottomleft"></th>
                        <td id="tbl-border-bottom">&nbsp;</td>
                        <th class="sized bottomright"></th>
                    </tr>
                </table>
                <div class="clear">&nbsp;</div>

            </div>
            <!--  end content -->
            <div class="clear">&nbsp;</div>
        </div>
        <!--  end content-outer........................................................END -->
            <?php
    footer();
    ?>

    </body>
</html>
    <?php
}else {
    header("Location: ../include/login.html");
}
?>